﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TravelWallet.Types;
using WindowsPhoneRecipes;
using System.Collections.ObjectModel;

namespace TravelWallet
{
    public partial class ListSelectionPage : PhoneApplicationPage
    {
        private string listPickerName;
        private string itemID;
        private ProgramCategory category;
        private bool hasBeenVisited;

        public ListSelectionPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //if (hasBeenVisited)
            //{
            //    this.lls_Selector.SelectionChanged -= lls_Selector_SelectionChanged;
            //    NavigationService.GoBack();
            //}

            //hasBeenVisited = true;

            if (NonLinearNavigationService.Instance.IsRecursiveBackNavigation == true)
            {
                return;
            }


            string categoryString;


            if (NavigationContext.QueryString.TryGetValue("category", out categoryString) &&
                NavigationContext.QueryString.TryGetValue("selectionName", out listPickerName) &&
                NavigationContext.QueryString.TryGetValue("itemID", out itemID))
            {

                categoryString = categoryString.ToLower();
                if (listPickerName == "programname")
                {
                    category = (ProgramCategory)Enum.Parse(typeof(ProgramCategory), categoryString, true);

                    if (category == ProgramCategory.Airlines)
                    {
                        this.PageTitle.Text = "Airline Programs";
                       
                            var airlinesByName = from airline in App.ViewModel.AirlineProgramNames
                                                 orderby airline.ToLower()
                                                 group airline by airline.ToLower()[0] into c
                                                 orderby c.Key
                                                 select new Group<string>(c.Key, c);

                            
                            if (this.lls_Selector.ItemsSource == null)
                            {
                                this.lls_Selector.ItemsSource = airlinesByName.ToList();
                            }
                        
                    }
                    else if (category == ProgramCategory.Hotels)
                    {
                        this.PageTitle.Text = "Hotel Programs";

                       
                            var hotelsByName = from hotel in App.ViewModel.HotelProgramNames
                                               orderby hotel.ToLower()
                                               group hotel by hotel.ToLower()[0] into c
                                               orderby c.Key
                                               select new Group<string>(c.Key, c);
                            if (this.lls_Selector.ItemsSource == null)
                            {
                                this.lls_Selector.ItemsSource = hotelsByName.ToList();
                            }
                        
                    }
                    else if (category == ProgramCategory.CarRentals)
                    {
                        this.PageTitle.Text = "Car Rental Programs";

                       
                            var carRentalsByName = from rental in App.ViewModel.CarRentalProgramNames
                                                   orderby rental.ToLower()
                                                   group rental by rental.ToLower()[0] into c
                                                   orderby c.Key
                                                   select new Group<string>(c.Key, c);

                            if (this.lls_Selector.ItemsSource == null)
                            {
                                this.lls_Selector.ItemsSource = carRentalsByName.ToList();
                            }
                        
                    }


                }
                else if (listPickerName == "name")
                {
                    category = (ProgramCategory)Enum.Parse(typeof(ProgramCategory), categoryString, true);

                    if (category == ProgramCategory.Airlines)
                    {
                        this.PageTitle.Text = "Airlines";

                       
                            var airlinesByName = from airline in App.ViewModel.AirlineName
                                                 orderby airline.ToLower()
                                                 group airline by airline.ToLower()[0] into c
                                                 orderby c.Key
                                                 select new Group<string>(c.Key, c);

                            if (this.lls_Selector.ItemsSource == null)
                            {
                                List<Group<string>> list = airlinesByName.ToList();
                                this.lls_Selector.ItemsSource = airlinesByName.ToList();
                            }
                        
                    }
                    else if (category == ProgramCategory.Hotels)
                    {
                        this.PageTitle.Text = "Hotels";

                      
                            var hotelsByName = from hotel in App.ViewModel.HotelNames
                                               orderby hotel.ToLower()
                                               group hotel by hotel.ToLower()[0] into c
                                               orderby c.Key
                                               select new Group<string>(c.Key, c);

                            if (this.lls_Selector.ItemsSource == null)
                            {
                                this.lls_Selector.ItemsSource = hotelsByName.ToList();
                            }
                        
                    }
                    else if (category == ProgramCategory.CarRentals)
                    {
                        this.PageTitle.Text = "Car Rentals";
                       
                            var carRentalsByName = from rental in App.ViewModel.RentalNames
                                                   orderby rental.ToLower()
                                                   group rental by rental.ToLower()[0] into c
                                                   orderby c.Key
                                                   select new Group<string>(c.Key, c);

                            if (this.lls_Selector.ItemsSource == null)
                            {
                                this.lls_Selector.ItemsSource = carRentalsByName.ToList();
                            }
                        
                    }

                  
                }

            }

           
        }

        internal static string CreateQueryString(long itemID, ProgramCategory programCategory, string p)
        {
            string url = "";
            
            url = string.Format("itemID={0}&category={1}&selectionName={2}", itemID, programCategory.ToString(), p);
            return url;
        }

        private void lls_Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lls_Selector.SelectedItem != null)
            {
                string selectedItem = lls_Selector.SelectedItem.ToString();
                string param = string.Format("itemID={0}&selectionName={1}&selectedItem={2}&category={3}", this.itemID, this.listPickerName, Uri.EscapeDataString(selectedItem), this.category);
                NavigationService.Navigate(new Uri("/AddEditProgram.xaml?" + param, UriKind.Relative));
            }
            
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            //navigate to the new page, passing a category in as well
            string param = string.Format("category={0}", this.category);
            NavigationService.Navigate(new Uri("/AddCustomProgramPage.xaml?" + param, UriKind.Relative));
        }


        public static ObservableCollection<T> ToObservableCollection<T>( IEnumerable<T> coll)
        {
            var c = new ObservableCollection<T>();
            foreach (var e in coll)
                c.Add(e);
            return c;
        }


        private void lls_Selector_Loaded(object sender, RoutedEventArgs e)
        {

            if (listPickerName == "name")
            {
                if (category == ProgramCategory.Airlines)
                {
                    var airlinesByName = from airline in App.ViewModel.AirlineName
                                         orderby airline.ToLower()
                                         group airline by airline.ToLower()[0] into c
                                         orderby c.Key
                                         select new Group<string>(c.Key, c);

                    this.lls_Selector.ItemsSource = airlinesByName.ToList();
                }
                else if (category == ProgramCategory.Hotels)
                {
                    var hotelsByName = from hotel in App.ViewModel.HotelNames
                                       orderby hotel.ToLower()
                                       group hotel by hotel.ToLower()[0] into c
                                       orderby c.Key
                                       select new Group<string>(c.Key, c);

                    this.lls_Selector.ItemsSource = hotelsByName.ToList();
                }
                else if (category == ProgramCategory.CarRentals)
                {
                    var carRentalsByName = from rental in App.ViewModel.RentalNames
                                           orderby rental.ToLower()
                                           group rental by rental.ToLower()[0] into c
                                           orderby c.Key
                                           select new Group<string>(c.Key, c);

                    this.lls_Selector.ItemsSource = carRentalsByName.ToList();
                }
                //check if we want to scroll the long list selector to a particular index
                if (AddCustomProgramPage.lastNameCreated != null)
                {
                    //we read the values out of this field
                    string program = AddCustomProgramPage.lastNameCreated;

                    this.lls_Selector.ScrollTo(program);

                    //and then set it null
                    AddCustomProgramPage.lastProgramNameCreated = null;
                    AddCustomProgramPage.lastNameCreated = null;
                }

            }
            else if (listPickerName == "programname")
            {
                if (category == ProgramCategory.Airlines)
                {
                    var airlinesByName = from airline in App.ViewModel.AirlineProgramNames
                                         orderby airline.ToLower()
                                         group airline by airline.ToLower()[0] into c
                                         orderby c.Key
                                         select new Group<string>(c.Key, c);

                    this.lls_Selector.ItemsSource = airlinesByName.ToList();
                }
                else if (category == ProgramCategory.Hotels)
                {
                    var hotelsByName = from hotel in App.ViewModel.HotelProgramNames
                                       orderby hotel.ToLower()
                                       group hotel by hotel.ToLower()[0] into c
                                       orderby c.Key
                                       select new Group<string>(c.Key, c);

                    this.lls_Selector.ItemsSource = hotelsByName.ToList();
                }
                else if (category == ProgramCategory.CarRentals)
                {
                    var carRentalsByName = from rental in App.ViewModel.CarRentalProgramNames
                                           orderby rental.ToLower()
                                           group rental by rental.ToLower()[0] into c
                                           orderby c.Key
                                           select new Group<string>(c.Key, c);

                    this.lls_Selector.ItemsSource = carRentalsByName.ToList();
                }

                //check if we want to scroll the long list selector to a particular index
                if (AddCustomProgramPage.lastProgramNameCreated != null)
                {
                    //we read the values out of this field
                    string program = AddCustomProgramPage.lastProgramNameCreated;

                    this.lls_Selector.ScrollTo(program);

                    //and then set it null
                    AddCustomProgramPage.lastProgramNameCreated = null;
                    AddCustomProgramPage.lastNameCreated = null;
                }

            }
        }

       
    }
}